﻿package view.bushuai.map.box
{
    import flash.display.*;
    import flash.geom.*;
    import view.bushuai.map.*;

    public class Pan_Tile extends Sprite
    {
        private var bmp_box:BitmapData;
        private var bmp:BitmapData;
        public static var self:Pan_Tile;

		public static var TestSprite:Sprite;
        public function Pan_Tile()
        {
			var _loc_4:Point;
            bmp = new BitmapData(MapInfo.Map_W, MapInfo.Map_H, true, 0);
			var t1 : Number = MapInfo.Map_W / MapInfo.Map_H;
			var _loc_1:Shape = new Shape();
			_loc_1.graphics.lineStyle(1, 255);
			
			var FromX:int = MapInfo.HarfTile_W
			var FromY:int = 0;
			var ToY:int =MapInfo.HarfTile_H;
			var ToX:int = 0;

			for (var i:int = 0; i < MapInfo.COLS + MapInfo.ROWS; i++)
			{
				_loc_1.graphics.moveTo(FromX, FromY);
				_loc_1.graphics.lineTo(ToX, ToY);
				FromX += MapInfo.Tile_W;
				ToY += MapInfo.Tile_H;
				if (FromX > MapInfo.Map_W)
				{
					FromX = MapInfo.Map_W;
					FromY += MapInfo.Tile_H ;
				}
			}
			
			FromX = MapInfo.Tile_W;
			FromY = MapInfo.Map_H;
			ToX = 0;
			ToY = MapInfo.Map_H - MapInfo.Tile_H;
			for (i = 0; i < MapInfo.COLS + MapInfo.ROWS; i++)
			{
				_loc_1.graphics.moveTo(FromX, FromY);
				_loc_1.graphics.lineTo(ToX, ToY);
				
				FromX += MapInfo.Tile_W;
				ToY -= MapInfo.Tile_H;
				if (FromX > MapInfo.Map_W)
				{
					FromX = MapInfo.Map_W;
					if (i == (MapInfo.COLS-2))
					{
						FromY -= (MapInfo.HarfTile_H)
					}
					else
					{
						FromY -= MapInfo.Tile_H ;
					}
				}
			}
			
			bmp.draw(_loc_1);
            var _loc_3:Bitmap = new Bitmap(bmp);
			
            addChild(_loc_3);
			self = this;
			
			TestSprite = new Sprite();
			TestSprite.graphics.beginFill(255);
			TestSprite.graphics.moveTo(MapInfo.HarfTile_W, 0);
			TestSprite.graphics.lineTo(0, MapInfo.HarfTile_H);
			TestSprite.graphics.lineTo(MapInfo.HarfTile_W, MapInfo.Tile_H);
			TestSprite.graphics.lineTo(MapInfo.Tile_W, MapInfo.HarfTile_H);
			TestSprite.graphics.lineTo(MapInfo.HarfTile_W, 0);
			TestSprite.graphics.endFill();
			TestSprite.x = 100;
			TestSprite.y = 600;
			//this.addChild(TestSprite);
			//var pp:Point = Pan_Tile.getCenterByPos(225);
			//self.bmp.floodFill(pp.x, pp.y, 4278255360);
			/*
            bmp_box = new BitmapData(MapInfo.Tile_W, MapInfo.Tile_H, true, 0);
            self = this;
            var _loc_1:Shape = new Shape();
            _loc_1.graphics.lineStyle(2, 255);

            drawTile(_loc_1);
            bmp_box.draw(_loc_1);
            bmp_box.floodFill(MapInfo.Tile_W / 2, MapInfo.Tile_H / 2, 65280);
            var _loc_2:*;
            while (_loc_2++ < MapInfo.ROWS * MapInfo.COLS)
            {
                // label
                _loc_4 = getPointByPos(_loc_2);
                bmp.copyPixels(bmp_box, new Rectangle(0, 0, MapInfo.Tile_W, MapInfo.Tile_H), new Point(_loc_4.x, _loc_4.y), null, null, true);
			}// end while
            var _loc_3:Bitmap = new Bitmap(bmp);
			
            addChild(_loc_3);
			
			*/
            return;
        }// end function

        public static function getPosByPt(PointX:int, PointY:int) : int
        {
			var ColIndex : int = (PointX / (MapInfo.HarfTile_W)) ;
			var RowIndex : int = (PointY / (MapInfo.HarfTile_H) );
			var ix : int;
			var iy : int;
			var ColTemp:int = (ColIndex+1) / 2;
			var RowTemp:int = RowIndex;
			
			if ((ColIndex + RowIndex) % 2 == 0)
			{
				ix  = ColIndex * (MapInfo.HarfTile_W);
				iy  = RowIndex * (MapInfo.HarfTile_H) + (MapInfo.HarfTile_H);
				
				if ( (Math.abs(PointY - iy) / Math.abs(PointX - ix)) < MapInfo.Angle)
				{
					RowTemp++
					TestSprite.x = ColIndex * (MapInfo.HarfTile_W);
					TestSprite.y = RowIndex * (MapInfo.HarfTile_H);
				}
				else
				{
					if (RowTemp % 2 != 0)
					{
						ColTemp --;
					}
					TestSprite.x = ColIndex * (MapInfo.HarfTile_W) - (MapInfo.HarfTile_W);
					TestSprite.y = RowIndex * (MapInfo.HarfTile_H) - (MapInfo.HarfTile_H);
				}
			}
			else
			{
				ix  = ColIndex * (MapInfo.HarfTile_W) + (MapInfo.HarfTile_W);
				iy = RowIndex * (MapInfo.HarfTile_H) + (MapInfo.HarfTile_H);
				
				if ( (Math.abs(PointY - iy) / Math.abs(PointX - ix)) < MapInfo.Angle)
				{
					RowTemp++
					if (RowTemp % 2 != 0)
					{
						ColTemp --;
					}
					TestSprite.x = ColIndex * (MapInfo.HarfTile_W) - (MapInfo.HarfTile_W);
					TestSprite.y = RowIndex * (MapInfo.HarfTile_H);
				}
				else
				{
					TestSprite.x = ColIndex * (MapInfo.HarfTile_W);
					TestSprite.y = RowIndex * (MapInfo.HarfTile_H) - (MapInfo.HarfTile_H);
				}
			}
			//TestSprite.x = PointX;
			//TestSprite.y = PointY;
			trace(ColTemp,RowTemp);
			return RowTemp * MapInfo.COLS + ColTemp;
        }// end function

        public static function changeST(draw:int, r:Point):void
        {
			if (r.x >= MapInfo.Map_W)
			{
				r.x = MapInfo.Map_W - 2;
			}
			if (r.y >= MapInfo.Map_H)
			{
				r.y = MapInfo.Map_H - 2;
			}
			self.bmp.floodFill(r.x, r.y, draw == 0 ? (65280) : (4278255360));
            return;
        }// end function

        //public static function drawTile(Tile_H:Shape) : void
        //{
           //var _loc_2:Graphics = Tile_H.graphics;
           //_loc_2.drawRect(0, 0, MapInfo.Tile_W, MapInfo.Tile_H);
           //return;
        //}// end function

        public static function getPointByPos(d:int) : Point
        {
            var RowIndex:int = d / MapInfo.COLS;
            var ColIndex:int = d % MapInfo.COLS;
			var ix:int = ColIndex * MapInfo.Tile_W;
			var iy:int = (RowIndex / 2) * MapInfo.Tile_H - (MapInfo.HarfTile_H);
			if (RowIndex% 2 == 0)
			{
				ix -= (MapInfo.Tile_W / 2);
			}
            return new Point(ix, iy);
        }// end function

        public static function getST(r:Point) : int
        {
            return self.bmp.getPixel32(r.x, r.y) > 4278190080 ? (1) : (0);
        }// end function

        public static function getCenterByPos(d:int) : Point
        {
           var _loc_2:Point = getPointByPos(d);
            _loc_2.x = _loc_2.x + MapInfo.HarfTile_W;
            _loc_2.y = _loc_2.y + MapInfo.HarfTile_H;
            return _loc_2;
        }// end function

    }
}
